﻿using System.Collections.Generic;
using System.Xml.Linq;
using BattleFury.Lib.Extensions;
using BattleFury.Lib.Input;
using Microsoft.Xna.Framework;

namespace BattleFury.Lib.Controls
{
	public class RadioButton
		: Control
	{
		public static Dictionary<string, List<RadioButton>> Groups = new Dictionary<string, List<RadioButton>>();

		private string _state = "CheckBox";

		public string Text { get; set; }
		public string Group { get; set; }
		public bool IsChecked { get; set; }
		public Color HighlightColor { get; set; }
		public Color ForegroundColor { get; set; }

		public RadioButton()
		{
			HighlightColor = Color.Yellow;
			Size = new Vector2(32f, 32f);
			ForegroundColor = Color.Black;
		}

		#region Serialization

		public override void Serialize()
		{
			throw new System.NotImplementedException();
		}

		public override void Deserialize(XElement element, object callingObject)
		{
			base.Deserialize(element, callingObject);

			Text = element.GetString("Text");
			Group = element.GetString("Group");
			IsChecked = element.GetBoolean("IsChecked");
			HighlightColor = element.GetColor("HighlightColor", Color.Yellow);
			ForegroundColor = element.GetColor("ForegroundColor", Color.Black);

			AddToGroup();
		}

		#endregion

		public override void Update(GameTime gameTime)
		{
			_state = "RadioButton";

			if (Input.Mouse.CurrentBounds.Intersects(Bounds))
			{
				_state = "RadioButtonHighlighted";
				if (Input.Mouse.IsMouseButtonPressed(MouseButtons.Left))
				{
					ToggleChecked();
					IsChecked = true;
				}
			}

			if (_state == "RadioButton" && IsChecked)
			{
				_state = "RadioButtonChecked";
			}
			else if (_state == "RadioButtonHighlighted" && IsChecked)
			{
				_state = "RadioButtonCheckedHighlighted";
			}

			if (!IsEnabled)
			{
				_state = "RadioButtonDisabled";
				if (IsChecked)
				{
					_state = "RadioButtonCheckedDisabled";
				}
			}
		}

		public override void Draw(GameTime gameTime)
		{
			var tint = Tint;
			if (_state.Contains("Highlighted"))
			{
				tint = HighlightColor;
			}
			SpriteBatch.Draw(Texture, Bounds, Skin[_state].SourceBounds, tint);

			if (Font != null)
			{
				var textSize = Font.MeasureString(Text);
				var textPosition = (Size / 2) - (textSize / 2);
				var offset = new Vector2(Skin[_state].Width + 2, 0);
				if (textPosition.X < 0) textPosition.X = 0;
				SpriteBatch.DrawString(Font, Text, Position + textPosition + Vector2.One + offset, Color.Black.WithOpacity(0.8f));
				SpriteBatch.DrawString(Font, Text, Position + textPosition + offset, ForegroundColor);
			}
		}

		private void AddToGroup()
		{
			if (Groups.ContainsKey(Group))
			{
				Groups[Group].Add(this);
			}
			else
			{
				Groups[Group] = new List<RadioButton> { this };
			}
		}

		private void ToggleChecked()
		{
			var group = Groups[Group];

			foreach (var checkBox in group)
			{
				if (checkBox != this)
				{
					checkBox.IsChecked = false;
				}
			}
		}
	}
}